﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using DataContracts;
using ServiceContracts;
using System.ServiceModel;



namespace Presention
{
    public partial class RoleDetails : System.Web.UI.Page
    {
        public DataContracts.RoleDTO roleDTO { get; set; }
        public enum RoleStatus
        {
            InUse = 1,
            NoUse = 2
        }

        /// <summary>
        /// Display role detail according to database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Get the role id from BatchUpdateRoleStatus.aspx
                string roleidUrlTrans = Request.QueryString["roleid_trans"];
                int role_Id = Convert.ToInt32(roleidUrlTrans);
                using (ServiceAgent SA = new ServiceAgent())
                {
                    this.CheckBoxList1.DataSource = SA.ServiceClient.GetAllAuthority();
                    this.CheckBoxList1.DataTextField = "AuthorityName";
                    this.CheckBoxList1.DataValueField = "AuthorityId";
                    this.CheckBoxList1.DataBind();


                    if (role_Id == 0)
                    {
                    }
                    else
                    {
                        if (SA.ServiceClient.GetRoleById(role_Id) == null)
                        {
                        }
                        else
                        {
                            roleDTO = SA.ServiceClient.GetRoleById(role_Id);
                            int selectStatusIdResult = (int)(roleDTO.Status.StatusId);

                            if (selectStatusIdResult == 1)
                            {
                                //// RadioButton1.Checked = true;
                                RadioButtonList1.Items[0].Selected = true;
                            }
                            if (selectStatusIdResult == 2)
                            {
                                //// RadioButton2.Checked = true;
                                RadioButtonList1.Items[1].Selected = true;
                            }
                            RoleName.Text = roleDTO.RoleName;

                            int authority_count = CheckBoxList1.Items.Count;
                            //use arraylist to save the select authority
                            ArrayList authorityaalllist = new ArrayList();
                            for (int i = 0; i < roleDTO.Authorities.Count; i++)
                            {
                                authorityaalllist.Add(roleDTO.Authorities.ElementAt(i).AuthorityId.ToString());

                            }
                            //Set checkboxlist select or not select according to database
                            for (int q = 0; q < CheckBoxList1.Items.Count; q++)
                            {
                                if (authorityaalllist.IndexOf(CheckBoxList1.Items[q].Value) != -1)
                                { CheckBoxList1.Items[q].Selected = true; }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Save Role detail
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void saverole_Click(object sender, EventArgs e)
        {

            if (CheckBoxList1.Items.Count == 0)
            {
                Response.Write("<script>alert('Please add role authority first!')</script>");
                return;
            }

            string roleid_temp = Request.QueryString["roleid_trans"];
            int role_Id = Convert.ToInt32(roleid_temp);
            try
            {
                using (ServiceAgent SA = new ServiceAgent())
                {
                    //Update role detail
                    if (role_Id > 0)
                    {
                        roleDTO = new RoleDTO();
                        roleDTO.RoleId = role_Id;

                        roleDTO.RoleName = RoleName.Text;

                        //// if (RadioButton1.Checked)
                        if (RadioButtonList1.Items[0].Selected)
                        {
                            roleDTO.Status = new StatusDTO();
                            roleDTO.Status.StatusId = Convert.ToInt32(RoleStatus.InUse);
                        }
                        //// if (RadioButton2.Checked)
                        if (RadioButtonList1.Items[1].Selected)
                        {
                            roleDTO.Status = new StatusDTO();
                            roleDTO.Status.StatusId = Convert.ToInt32(RoleStatus.NoUse);
                        }

                        roleDTO.Authorities = new List<AuthorityDTO>();
                        roleDTO.Authorities.Clear();
                        int count = CheckBoxList1.Items.Count;
                        for (int i = 0; i < count; i++)
                        {
                            if (CheckBoxList1.Items[i].Selected)
                            {
                                AuthorityDTO authority = new AuthorityDTO();
                                authority.AuthorityName = CheckBoxList1.Items[i].Text;
                                authority.AuthorityId = Convert.ToInt32(CheckBoxList1.Items[i].Value);
                                roleDTO.Authorities.Add(authority);
                            }
                        }

                        SA.ServiceClient.UpdateRole(roleDTO);
                    }
                    //Add new role
                    if (role_Id == 0)
                    {
                        roleDTO = new RoleDTO();

                        roleDTO.RoleName = RoleName.Text;

                        ////if (RadioButton1.Checked)
                        if (RadioButtonList1.Items[0].Selected)
                        {
                            roleDTO.Status = new StatusDTO();
                            roleDTO.Status.StatusId = Convert.ToInt32(RoleStatus.InUse);
                        }
                        //// if (RadioButton2.Checked)
                        if (RadioButtonList1.Items[1].Selected)
                        {
                            roleDTO.Status = new StatusDTO();
                            roleDTO.Status.StatusId = Convert.ToInt32(RoleStatus.NoUse);
                        }
                        roleDTO.Authorities = new List<AuthorityDTO>();
                        int count = CheckBoxList1.Items.Count;
                        for (int i = 0; i < count; i++)
                        {
                            if (CheckBoxList1.Items[i].Selected)
                            {
                                AuthorityDTO authority = new AuthorityDTO();
                                authority.AuthorityName = CheckBoxList1.Items[i].Text;
                                authority.AuthorityId = Convert.ToInt32(CheckBoxList1.Items[i].Value);
                                roleDTO.Authorities.Add(authority);
                            }
                        }
                        SA.ServiceClient.AddNewRole(roleDTO);
                    }
                    Response.Write("<script>alert('Success!')</script>");
                    Response.Redirect("BatchUpdateRoleStatus.aspx");
                }
            }
            catch (FaultException ex)
            {
                Response.Write("<script language=javascript>alert('" + ex.Message + "')</script>");
                using (ServiceAgent SA = new ServiceAgent())
                {
                    this.CheckBoxList1.DataSource = SA.ServiceClient.GetAllAuthority();
                    this.CheckBoxList1.DataTextField = "AuthorityName";
                    this.CheckBoxList1.DataValueField = "AuthorityId";
                    this.CheckBoxList1.DataBind();
                }
            }
        }

    }
}